package com.project.class4_8.proxy2.dao.impl;

import java.sql.*;

public class BaseDao {
    protected Connection con;
    protected PreparedStatement ps;
    protected ResultSet rs;

    /**
     * 创建并返回连接
     * @return 连接对象
     */
    public static Connection getConnection(){
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection c = DriverManager.getConnection(
                    "jdbc:mysql://localhost:12345/mydb?characterEncoding=utf-8",
                    "root","lovo");
            return c;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 设置连接
     * @param con 连接对象
     */
    public void setConnection(Connection con){
        this.con = con;
    }

    public void updateData(String sql,Object... valueArray){
        try {
            ps = con.prepareStatement(sql);
            for(int i=0;i<valueArray.length;i++){
                ps.setObject(i+1,valueArray[i]);
            }

            ps.executeUpdate();

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
